<template>
  <div>
    <el-card>
      <el-row class="top_wrap">
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <el-table :data="rolesList" style="width: 100%" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item,index) in scope.row.children"
              :key="item.pid"
              :class="['tag_bot',index===0?'ftag_top':'','tag_cent']"
            >
              <el-col :span="5">
                <el-tag closable @close="handleRightRemove(scope.row,item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  v-for="(item2,index2) in item.children"
                  :key="item2.pid"
                  :class="['tag_bot',index2===0?'':'ftag_top','tag_cent']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="handleRightRemove(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.pid"
                      type="warning"
                      closable
                      @close="handleRightRemove(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="id" label="编号" width="180"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="180"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>

            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="handleDispatchRight(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="分配权限"
      :visible.sync="isShowRolesSetting"
      width="60%"
      @close="handleCloseRightsDialog"
    >
      <el-tree
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defaultChecked"
        :data="rightsList"
        :props="defaultProps"
        ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowRolesSetting = false">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [],
      isShowRolesSetting: false,
      rightsList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defaultChecked: [],
      roleId: 0
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      console.log(res)

      //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rolesList = res.data
      //   this.$message.success(res.meta.msg)
    },
    async handleRightRemove(role, rightId) {
      const confirmRes = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(error => error)
      if (confirmRes !== 'confirm') return this.$message.info('已取消删除!')

      const { data: res } = await this.$http.delete(
        `roles/${role.id * 1}/rights/${rightId * 1}`
      )

      if (res.meta.status !== 200) return this.$message.info(res.meta.msg)
      this.$message.success(res.meta.msg)
      role.children = res.data
      //   this.getRolesList()
    },
    async handleDispatchRight(item) {
      this.roleId = item.id
      const { data: res } = await this.$http.get('rights/tree ')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rightsList = res.data
      this.getLeaf(item, this.defaultChecked)
      console.log(this.defaultChecked)
      this.isShowRolesSetting = true
    },
    getLeaf(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeaf(item, arr)
      })
    },
    handleCloseRightsDialog() {
      this.defaultChecked = []
    },
    async handleDialogConfirm() {
      console.log(this.roleId, '------------')

      const ref = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const idsStr = ref.join(',')
      const { data: res } = await this.$http.post(
        'roles/' + this.roleId + '/rights',
        {
          ids: idsStr
        }
      )
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getRolesList()
      this.isShowRolesSetting = false
    }
  }
}
</script>

<style lang="less" scoped>
.top_wrap {
  margin-bottom: 15px;
}

.ftag_top {
  border-top: solid #eee 1px;
}
.tag_bot {
  border-bottom: solid #eee 1px;
  padding: 10px;
}
.el-tag {
  margin-right: 10px;
}
.tag_cent {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>